package com.moon.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.moon.common.query.Expression;

@SuppressWarnings({"unchecked","unused"})
public class CommonQuery {
	
	Class clazz;
	HashMap<String,Object> params;
	private List<Expression> expressions;
	private String fromExpression;
	private String whereExpression;
	private int count;
	
	public CommonQuery(Class clazz) {
		this.clazz = clazz;
		this.count=0;
		this.params = new HashMap();
		this.expressions = new ArrayList<Expression>();
		this.fromExpression = "from " + clazz.getSimpleName();
		this.whereExpression = "";
	}
	
	public boolean add(Expression exp){
		String temp = "";
		params.put(exp.getFieldName(), exp.getValue());
		if(count==0){
			temp = "where "+exp.getTemplate();
		}else{
			temp = exp.getAndOrType()+""+exp.getTemplate();
		}
		whereExpression+=temp;
		count++;
		expressions.add(exp);
		return false;
	}

	public String getHql() {
		return fromExpression+whereExpression;
	}

	public HashMap<String, Object> getParams() {
		return params;
	}
	
}
